<?php

namespace App\Admin\Metrics\Examples;

use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Donut;

class PaperDevices extends Donut
{
//    protected $labels = ['A：男', 'B：女', 'C：女'];
    protected $contentWidth = [9, 3];

    /**
     * @var array
     * [
     *  'title' => '1：您的性别',
     *  'sub_title' => 31,
     *  'labels' => ['A：男', 'B：女', 'C：女'],
     *  'chart_content' => [15, 16, 17],
     *  'show_num' => ['45%（15人）', '55%（16人）', '55%（16人）'],
     * ]
     */
    protected $pageData;

    protected $colors;


    public function __construct($title = null, $icon = null, $pageData = [])
    {
        $color = Admin::color();
        $this->colors = [
            $color->primary(),
            $color->alpha('blue2', 0.5),
            $color->green(),
            $color->blue2(),
            $color->gray(),
            $color->orange1(),
            $color->redDarker(),
            $color->yellow(),
            $color->blue1(),
            $color->orange2(),
            $color->pink(),
        ];

        $this->pageData = $pageData;
        parent::__construct($title, $icon);
    }


    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();


        $this->title($this->pageData['title']);
        $this->subTitle('共计' . $this->pageData['sub_title'] . '人');
        $this->chartLabels($this->pageData['labels']);
        // 设置图表颜色
        $this->chartColors($this->colors);
    }

    /**
     * 渲染模板
     *
     * @return string
     */
    public function render()
    {
        $this->fill();

        return parent::render();
    }

    /**
     * 写入数据.
     *
     * @return void
     */
    public function fill()
    {
        $this->withContent($this->pageData['show_num']);

        // 图表数据
        $this->withChart($this->pageData['chart_content']);
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data
        ]);
    }

    /**
     * 设置卡片头部内容.
     *
     * @param array $showNumContent
     *
     * @return $this
     */
    protected function withContent($showNumContent = [])
    {
        $labelStyle = 'width: 70%';

        $html = '';
        foreach ($showNumContent as $showNumContentKey => $showNumContentVal) {
            $style = count($showNumContent) == $showNumContentKey + 1 ? 'margin-bottom: 8px' : '';
            $html .= '
            <div class="d-flex pl-1 pr-1 pt-1" style="'.$style.'">
                <div style="'.$labelStyle.'">
                    <i class="fa fa-circle" style="color: '.$this->colors[$showNumContentKey].'"></i> '.$this->pageData['labels'][$showNumContentKey].'
                </div>
                <div>'.$showNumContentVal.'</div>
            </div>
            ';
        }

        return $this->content(
            <<<HTML
                {$html}
HTML
        );
    }
}
